﻿using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ExtensionLibrary
{
    [RunInstaller(true)]
    public partial class InstallerClass : System.Configuration.Install.Installer
    {
        private const string ClientName = "KaoshiduanExamClient";
        private const string ClientExeName = "ExamClient.exe";
        public InstallerClass()
        {
            InitializeComponent();
            this.AfterInstall += new InstallEventHandler(InstallerDemo_AfterInstall);
            this.AfterUninstall += new InstallEventHandler(InstallerDemo_AfterUninstall);
        }
        private string targetdirPath = "";
        /// <summary>
        /// 安装完成后触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InstallerDemo_AfterInstall(object sender, InstallEventArgs e)
        {
            //throw new NotImplementedException();
            targetdirPath = this.Context.Parameters["targetdir"].TrimEnd('\\');//获取用户设定的安装目标路径, 注意，需要在Setup项目里面自定义操作的属性栏里面的CustomActionData添加上/targetdir="[TARGETDIR]\"
            //Msg("安装路径：" + targetdirPath);
            var result = RegeditAdd(ClientName, Path.Combine(targetdirPath, ClientExeName), "");
        }
        /// <summary>
        /// 卸载完成之后触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InstallerDemo_AfterUninstall(object sender, InstallEventArgs e)
        {
            //throw new NotImplementedException();
            var result = RegeditDelete(ClientName);
        }
        /// <summary>
        /// 注册协议
        /// </summary>
        /// <param name="Root_Key">根节点</param>
        /// <param name="file_application_path">应用程序路径</param>
        /// <param name="file_application_ico">应用程序打开图标,可选值</param>
        /// <returns></returns>
        public bool RegeditAdd(string Root_Key, string file_application_path, string file_application_ico)
        {
            //获取注册表HKEY_CLASSES_ROOT
            RegistryKey reg_ClassRoot = Registry.ClassesRoot;
            try
            {
                RegistryKey reg_key = reg_ClassRoot.OpenSubKey(Root_Key, true);
                if (reg_key == null)
                {
                    //创建子节点HKEY_CLASSES_ROOT\tpswftest
                    RegistryKey reg_sjbs = reg_ClassRoot.CreateSubKey(Root_Key);
                    //添加默认项
                    reg_sjbs.SetValue("", "URL:" + Root_Key + " Protocol Handler");
                    //协议别名
                    reg_sjbs.SetValue("URL Protocol", "");
                    //创建[HKEY_CLASSES_ROOT\tpswftest\DefaultIcon]
                    RegistryKey reg_DefaultIcon = reg_sjbs.CreateSubKey("DefaultIcon");
                    if (!String.IsNullOrEmpty(file_application_ico))
                    {
                        //设置自定义图标
                        reg_DefaultIcon.SetValue("", file_application_ico);
                    }
                    else
                    {
                        //设置系统定义图标
                        reg_DefaultIcon.SetValue("", file_application_path);
                    }
                    //创建呼出处理程序[HKEY_CLASSES_ROOT\tpswftest\shell\open\command]
                    RegistryKey reg_command = reg_sjbs.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
                    //%1 表示传递的参数，再次%1表示调用处显示链接文本
                    reg_command.SetValue("", file_application_path + " %1");
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally { reg_ClassRoot.Close(); }
        }
        /// <summary>
        /// 删除协议
        /// </summary>
        /// <param name="Root_Key">根节点</param>
        /// <returns></returns>
        public bool RegeditDelete(string Root_Key)
        {
            //获取注册表HKEY_CLASSES_ROOT
            RegistryKey reg_ClassRoot = Registry.ClassesRoot;
            try
            {
                //获取注册表[HKEY_CLASSES_ROOT\tpswftest]项
                RegistryKey reg_sjbs = reg_ClassRoot.OpenSubKey(Root_Key, true);
                if (reg_sjbs != null)
                {
                    reg_ClassRoot.DeleteSubKeyTree(Root_Key);
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch
            {
                //添加错误日志
                return false;
            }
            finally { reg_ClassRoot.Close(); }
        }

    }
}